package com.rxnetwork.sample.utils.interceptor

import okhttp3.*
import okio.*
import java.io.IOException

/**
 * 接口请求拦截添加base参数
 */
class AppHeaderInterceptor : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val oldRequest = chain.request()
        if (oldRequest.body() == null || oldRequest.header("Content-Encoding") != null) {
            return chain.proceed(oldRequest)
        }
        // 添加url后的参数
        val authorizedUrlBuilder = oldRequest
                .url()
                .newBuilder()
                .scheme(oldRequest.url().scheme()).host(oldRequest.url().host())
        //                .setEncodedQueryParameter("lversion", "10.0")
        //                .setEncodedQueryParameter("token", "token---sadsaff")
        //                .setQueryParameter("user", "xiaocode")
        //                .setQueryParameter("devices", "android");
        // 新的请求
        val newRequest = oldRequest.newBuilder()
                .url(authorizedUrlBuilder.build())
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Accept-Encoding", "gzip, deflate")
                .addHeader("Connection", "keep-alive")
                .addHeader("Accept", "*/*")
                .addHeader("Cookie", "add cookies here")
                //若是服务端没有开启gzip则，服务端解析不到
                //                .method(oldRequest.method(), gzip(oldRequest.body()))
                .method(oldRequest.method(), oldRequest.body())
                .build()
        val response = chain.proceed(newRequest)
        val mediaType = response.body().contentType()
        //服务端开启gzip则客户端配置开启gzip
        //        String content = unGzip(response.body()).toString();
        val content = response.body().toString()
        if (response.body() != null) {
            return response.newBuilder().body(ResponseBody.create(mediaType, content)).build()
        } else {
            return response
        }
    }

    //gzip压缩
    private fun gzip(body: RequestBody): RequestBody {
        return object : RequestBody() {
            override fun contentType(): MediaType {
                return body.contentType()
            }

            override fun contentLength(): Long {
                return -1 // 无法知道压缩后的数据大小
            }

            @Throws(IOException::class)
            override fun writeTo(sink: BufferedSink) {
                val gzipSink = Okio.buffer(GzipSink(sink))
                body.writeTo(gzipSink)
                gzipSink.close()
            }
        }
    }

    //解压缩
    private fun unGzip(body: ResponseBody): ResponseBody {
        return object : ResponseBody() {

            override fun contentType(): MediaType {
                return body.contentType()
            }

            override fun contentLength(): Long {
                return -1
            }

            override fun source(): BufferedSource {
                return Okio.buffer(GzipSource(body.source()))
            }
        }
    }
}
